package org.makcro.sms.services.chat.web.client.widgets;

import org.makcro.sms.services.chat.web.client.AppEvents;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.event.dom.client.KeyCodes;

public class LoginDialog extends Dialog {
	private TextField<String> txtPassword;
	private TextField<String> txtUsername;
	private Button btnLogin;
	private Button btnReset;

	public LoginDialog() {

		setSize("298px", "155px");
		setResizable(false);
		setButtons("");
		setModal(true);
		setHeading("Prijava");
		FormLayout formLayout = new FormLayout();
		formLayout.setLabelWidth(60);
		formLayout.setDefaultWidth(155);
		setLayout(formLayout);
		setBodyBorder(true);
		setBodyStyle("padding: 8px;background: none");
		setClosable(false);
		
		txtUsername = new TextField<String>();

		txtUsername.setMinLength(4);
		FormData fd_txtUsername = new FormData("95%");
		fd_txtUsername.setMargins(new Margins(10, 0, 0, 0));
		add(txtUsername, fd_txtUsername);
		txtUsername.setFieldLabel("Korisnik");

		txtPassword = new TextField<String>();
		txtPassword.setMinLength(4);
		txtPassword.setPassword(true);
		add(txtPassword, new FormData("95%"));
		txtPassword.setFieldLabel("Lozinka");

		KeyListener enterKeyListener = new KeyListener() {
			public void componentKeyUp(ComponentEvent event) {
				if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
					LoginDialog.this.onLogin();
				}
			}
		};
		
		btnLogin = new Button("Prijava");
		btnLogin.addListener(Events.Select, new Listener<ButtonEvent>() {
			public void handleEvent(ButtonEvent e) {
				onLogin();
			}
		});
		txtPassword.addKeyListener(enterKeyListener);
		
		addButton(btnLogin);

		btnReset = new Button("Ponisti");
		btnReset.addListener(Events.Select, new Listener<ButtonEvent>() {
			public void handleEvent(ButtonEvent e) {
				reset();
				txtUsername.focus();
			}
		});
		addButton(btnReset);

	}

	protected void onLogin() {
		getButtonBar().disable();
		String[] loginData = new String[] { txtUsername.getValue(), txtPassword.getValue() };

		Dispatcher.get().dispatch(AppEvents.ProcessLogin, loginData);
	}

	protected boolean hasValue(TextField<String> field) {
		return field.getValue() != null && field.getValue().length() > 0;
	}

	protected void validate() {

	}

	public TextField<String> getTxtUsername() {
		return txtUsername;
	}

	public void reset() {
		txtUsername.reset();
		txtPassword.reset();
	}
	
}
